﻿// -----------------------------------------------------------------------
// <copyright file="InvoiceDetail_DAL.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace PMSGSC.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using PMSGSC.Common;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class InvoiceDetail_DAL:BaseDAL
    {


        // Lấy tất cả InvoiceDetail
        public static List<InvoiceDetail> GetAll(int id)
        {
            DB.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, DB.InvoiceDetails);
            return DB.InvoiceDetails.Where(n=>n.Invoice_ID == id).ToList();
        }
        public static List<InvoiceDetail> ReportStockOut(DateTime From, DateTime To)
        {
            return DB.InvoiceDetails.Where(n => n.Invoice.InvoiceDate.Value.Date.CompareTo(From) >= 0 && n.Invoice.InvoiceDate.Value.Date.CompareTo(To) <= 0).ToList();
        }
        public static InvoiceDetail GetNewInvoiceDetail()
        {
            return DB.InvoiceDetails.ToArray().LastOrDefault();
        }
        public static List<InvoiceDetail> GetDataByWorkOrderID(int woID)
        {
            Invoice invoice = DB.Invoices.Where(n => n.WorkOrder_ID == woID).FirstOrDefault();
            if (invoice != null)
                return DB.InvoiceDetails.Where(n => n.Invoice_ID == invoice.Invoice_ID).ToList();

            return null;
        }
        // Lấy InvoiceDetail theo Id
        public static List<InvoiceDetail> GetDataById(int id)
        {
            return DB.InvoiceDetails.Where(n => n.Invoice_ID == id).ToList();
        }
        //Thêm một InvoiceDetail
        public static bool Insert(InvoiceDetail InvoiceDetail)
        {
            try
            {
                DB.InvoiceDetails.InsertOnSubmit(InvoiceDetail);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Xóa một InvoiceDetail
        public static bool Delete(InvoiceDetail InvoiceDetail)
        {
            try
            {
                DB.InvoiceDetails.DeleteOnSubmit(InvoiceDetail);
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        // Cập nhật InvoiceDetail
        public static bool Update(InvoiceDetail InvoiceDetail)
        {
            try
            {
                //  var result = DB.Stocks.Single(c => c.Stock_ID == InvoiceDetail.Stock_ID);
                //  DB.InvoiceDetail.First().Stock = result;
                //  DB.InvoiceDetail.First().Employee_ID = InvoiceDetail.Employee_ID;
                //  DB.InvoiceDetail.First().ImportStock_ID = InvoiceDetail.ImportStock_ID;
                //  DB.InvoiceDetail.First().IEType = InvoiceDetail.IEType;
                //  DB.InvoiceDetail.First().InvoiceDetailDate = InvoiceDetail.InvoiceDetailDate;
                //  DB.InvoiceDetail.First().InvoiceDetailCode = InvoiceDetail.InvoiceDetailCode;
                DB.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static int GetOrderNumberVoucher()
        {
            return DB.InvoiceDetails.Count();
        }
        public static List<InvoiceDetail> ConsumableStock(DateTime From, DateTime To, string StockName, bool TransType)
        {
            var predicate = PredicateBuilder.True<InvoiceDetail>();
            if (From != null && To != null)
                predicate = predicate.And(n => n.Invoice.InvoiceDate.Value.Date.CompareTo(From) >= 0 && n.Invoice.InvoiceDate.Value.Date.CompareTo(To) <= 0);
            if (StockName != "All")
                predicate = predicate.And(n => n.Stock.StockName == StockName);


            predicate = predicate.And(n => n.Invoice.Type == TransType);

            return DB.InvoiceDetails.Where(predicate).Cast<InvoiceDetail>().ToList(); ;
        }
       
    }
}
